package com.yang.learn;

import com.google.protobuf.InvalidProtocolBufferException;
import com.googlecode.protobuf.format.JsonFormat;
import com.yang.learn.AddressBookProtos.AddressBook;
import com.yang.learn.AddressBookProtos.Person;
import com.yang.learn.AddressBookProtos.Person.PhoneNumber;

/**
 * @ClassName: Pb2Learn
 * @Description:
 * @Author: zhangyang (zhangyang04@qiyi.com)
 * @CreateDate: 2019-03-14 15:39
 */

public class Pb2Learn {
    public static void main(String[] args) throws InvalidProtocolBufferException {
        Person person = Person.newBuilder()
                .setId(1)
                .setName("lin")
                .setEmail("245393886@qq.com")
                .addPhones(PhoneNumber.newBuilder().setNumber("15218612888")
                        .setType(Person.PhoneType.WORK))
                .build();
        AddressBook addressBook = AddressBook.newBuilder()
                .addPeople(person)
                .build();
        //序列化之前
        System.out.println("序列化之前  " + addressBook.toString());
        System.out.println(JsonFormat.printToString(addressBook));
        //序列化
        byte[] addressBook_se = addressBook.toByteArray();
        System.out.println("序列化  " + addressBook_se);


        //反序列化
        AddressBook AddressBook_de = AddressBook.parseFrom(addressBook_se);
        System.out.println("反序列化 " + AddressBook_de);

    }

}
